﻿using System;
using System.Windows;

namespace MVVMFramework.View
{
    public class BoolToGridLengthConverter : ConverterBase
    {
        public GridLength TrueValue
        {
            get;
            set;
        }

        public GridLength FalseValue
        {
            get;
            set;
        }

        protected override object TryConvert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool booleanValue = System.Convert.ToBoolean(value, culture);

            if (booleanValue)
                return TrueValue;

            return FalseValue;
        }

        protected override object TryConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (!(value is GridLength))
                throw new ConverterException("Unexpected value type");

            GridLength length = (GridLength)value;
            bool convertedValue = (length == TrueValue);
            return convertedValue;
        }
    }
}
